

const BG_IMG_URL = 'images/plane/bg.jpg';

define([
  'require',
  'score'
], function (require, score) {
  'use strict';
  
  var _this = {};

  // 添加bg到stage
  function init (app, container) {
    var self = _this;
    self.screen = app.screen;
    self.loading = true;
    self.score = score;
    
    new PIXI.loaders.Loader()
      .add('bgImg', BG_IMG_URL)
      .load(function (loader, res) {
        self.bgTex = PIXI.Texture.from('bgImg');
        self.bg1 = new PIXI.Sprite(self.bgTex);
        self.bg2 = new PIXI.Sprite(self.bgTex);

        self.bg1.width = self.screen.width;
        self.bg1.height = self.screen.height;
        self.bg1.position.set(0, 0);
  
        self.bg2.width = self.screen.width;
        self.bg2.height = self.screen.height;
        self.bg2.position.set(0, -self.screen.height);
  
        container.addChild(self.bg1);
        container.addChild(self.bg2);
        self.loading = false;
        score.init(app, app.stage);
      });
  }

  // ticker中更新
  function update(delta) {
    //console.info('bg1:' + bg1.position.y + 'bg2:' + bg2.position.y)
    if (_this.loading) {
      return;
    }

    let curr = _this.bg1.position.y;
    curr += delta * 2;

    if (curr >= _this.bg1.height) {
      curr = -_this.screen.height;
    }
    _this.bg1.position.y = curr;

    if (curr >= 0) {
      _this.bg2.position.y = curr - _this.bg2.height;
    } else {
      _this.bg2.position.y = curr + _this.bg2.height;
    }

  }

  return {
    init: init,
    update: update,
    data: _this
  }

});
